/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kungkhies.desktop.activity.viewer;

import com.kungkhies.desktop.library.entities.Activity;
import java.util.Collection;
import javax.swing.SwingUtilities;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.util.RequestProcessor;
import org.openide.util.lookup.AbstractLookup;
import org.openide.util.lookup.InstanceContent;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//com.kungkhies.desktop.activity.viewer//ActivityViewer//EN",
autostore = false)
@TopComponent.Description(preferredID = "ActivityViewerTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "editor", openAtStartup = false)
@ActionID(category = "Window", id = "com.kungkhies.desktop.activity.viewer.ActivityViewerTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_ActivityViewerAction",
preferredID = "ActivityViewerTopComponent")
public final class ActivityViewerTopComponent extends TopComponent implements LookupListener
{
    private Activity activity;
    private final InstanceContent content;
    private Lookup.Result result = null;

    public ActivityViewerTopComponent()
    {
        initComponents();
        setName(NbBundle.getMessage(ActivityViewerTopComponent.class, "CTL_ActivityViewerTopComponent"));
        setToolTipText(NbBundle.getMessage(ActivityViewerTopComponent.class, "HINT_ActivityViewerTopComponent"));


        //Create a new instance of our dynamic object:
        content = new InstanceContent();

        //Add the dynamic object to the TopComponent Lookup:
        associateLookup(new AbstractLookup(content));

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        urlLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descriptionTextArea = new javax.swing.JTextArea();
        nameTextLabel = new javax.swing.JLabel();
        urlTextLabel = new javax.swing.JLabel();

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(ActivityViewerTopComponent.class, "ActivityViewerTopComponent.jLabel1.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, org.openide.util.NbBundle.getMessage(ActivityViewerTopComponent.class, "ActivityViewerTopComponent.jLabel2.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(urlLabel, org.openide.util.NbBundle.getMessage(ActivityViewerTopComponent.class, "ActivityViewerTopComponent.urlLabel.text")); // NOI18N

        descriptionTextArea.setBackground(javax.swing.UIManager.getDefaults().getColor("Button.background"));
        descriptionTextArea.setColumns(20);
        descriptionTextArea.setRows(5);
        descriptionTextArea.setFocusable(false);
        jScrollPane1.setViewportView(descriptionTextArea);

        org.openide.awt.Mnemonics.setLocalizedText(nameTextLabel, org.openide.util.NbBundle.getMessage(ActivityViewerTopComponent.class, "ActivityViewerTopComponent.nameTextLabel.text")); // NOI18N

        org.openide.awt.Mnemonics.setLocalizedText(urlTextLabel, org.openide.util.NbBundle.getMessage(ActivityViewerTopComponent.class, "ActivityViewerTopComponent.urlTextLabel.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(73, 73, 73)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(urlTextLabel)
                        .addContainerGap())
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(nameTextLabel)
                            .addContainerGap())
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addContainerGap())
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 565, Short.MAX_VALUE)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(urlLabel, javax.swing.GroupLayout.Alignment.LEADING))
                                .addGap(118, 118, 118))))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(66, 66, 66)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(nameTextLabel)
                .addGap(39, 39, 39)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addComponent(urlLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(urlTextLabel)
                .addContainerGap(375, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea descriptionTextArea;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel nameTextLabel;
    private javax.swing.JLabel urlLabel;
    private javax.swing.JLabel urlTextLabel;
    // End of variables declaration//GEN-END:variables

    @Override
    public void componentOpened()
    {     
        RequestProcessor.getDefault().post(new Runnable()
        {
            @Override
            public void run()
            {
                readActivity();
            }
        });     
    }

    @Override
    public void componentClosed()
    {
        result.removeLookupListener(this);
        result = null;  
    }

    void writeProperties(java.util.Properties p)
    {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    private void readActivity()
    {
        //JavaDBSupport.ensureStartedDB();
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                TopComponent tc = WindowManager.getDefault().findTopComponent("ActivityExplorerTopComponent");
                if (tc == null)
                {
                    System.out.println("Activity Viewer. readActivity: top component in get default is null");
                    return;
                }
                result = tc.getLookup().lookupResult(Activity.class);
                result.addLookupListener(ActivityViewerTopComponent.this);
                resultChanged(new LookupEvent(result));
            }
        });
    }

    void readProperties(java.util.Properties p)
    {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    public void resultChanged(LookupEvent lookupEvent)
    {
        Lookup.Result r = (Lookup.Result) lookupEvent.getSource();
        Collection<Activity> coll = (Collection<Activity>) r.allInstances();
        if (!coll.isEmpty())
        {
            for (Activity ac : coll)
            {
                activity = ac;
                String nameMod = ac.getName();
                String ass = ac.getAssessment();
                boolean assIncluded = false;
                if (ass != null)
                {
                    assIncluded = Boolean.parseBoolean(ass.trim());
                }
                else
                {
                    
                }
                
                if (assIncluded)
                {
                    nameMod = nameMod + "  (assessment included)";
                }
                else
                {
                    nameMod = nameMod + "  (no assessment included)"; 
                }
                
                nameTextLabel.setText(nameMod);
                descriptionTextArea.setText(ac.getDescription());
                urlTextLabel.setText(ac.getUrl());           
            }
        }
        else
        {
            nameTextLabel.setText("");
            descriptionTextArea.setText("");
            urlTextLabel.setText("");   
        }
    }

    public void resetFields()
    {
        nameTextLabel.setText("");
        descriptionTextArea.setText("");
        urlTextLabel.setText("");   
    }
}
